import { setToken, getToken, removeToken, setUserInfo, removeUserInfo, setStamp } from '@/utils/auth'
import { login, getUserInfo, getUserInfoById } from '@/api/user'
const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  setToken(state, playload) {
    state.token = playload
    setToken(playload)
  },
  removeToken(state) {
    state.token = null
    removeToken()
  },
  // 获取用户信息
  setUserInfo(state, playload) {
    state.userInfo = playload
    setUserInfo(playload)
  },
  removeUserInfo(state) {
    state.userInfo = {}
    removeUserInfo()
  }
}
const actions = {
  async login(context, playload) {
    const result = await login(playload)
    context.commit('setToken', result)
    setStamp()
  },
  async getUserInfo(context) {
    // const aaa = await Promise.all([getUserInfo(), getUserInfo()])
    // const bbb = await Promise.all([() => [1, 2, 3], () => [1, 2, 3]])
    // console.log(aaa)
    // console.log(bbb)
    const userInfo = await getUserInfo()
    const userInfoById = await getUserInfoById(userInfo.userId)
    context.commit('setUserInfo', { ...userInfo, ...userInfoById })
  },
  logout(context) {
    context.commit('removeToken')
    context.commit('removeUserInfo')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
